package cc.idoone.spring_boot_hello.upload;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class UploadConfiguration extends WebMvcConfigurerAdapter {

	@Value("${spring.upload.multipart.maxInMemorySize}")
	private int maxInMemorySize;

	@Value("${spring.upload.multipart.maxUploadSize}")
	private long maxUploadSize;

	@Value("${spring.upload.multipart.maxUploadSizePerFile}")
	private long maxUploadSizePerFile;

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver multipart = new CommonsMultipartResolver();
		multipart.setMaxInMemorySize(maxInMemorySize);
		multipart.setMaxUploadSize(maxUploadSize);
		multipart.setMaxUploadSizePerFile(maxUploadSizePerFile);
		return multipart;
	}

	@Bean
	@Order(0)
	public MultipartFilter multipartFilter() {
		MultipartFilter multipartFilter = new MultipartFilter();
		multipartFilter.setMultipartResolverBeanName("multipartResolver");
		return multipartFilter;
	}
}